package com.hopu.v1.controller.front.course;

import com.hopu.v1.pojo.common.R;
import com.hopu.v1.pojo.course.CourseFeedback;
import com.hopu.v1.pojo.course.FCourseHomeworkRecord;
import com.hopu.v1.service.front.course.CourseFeedbackService;
import com.hopu.v1.service.front.course.CourseService;
import com.hopu.v1.service.front.course.FCourseHomeworkRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/front/course")
public class CourseController {
    @Autowired
    private CourseService courseService;
    @Autowired
    private CourseFeedbackService courseFeedbackService;
    @Autowired
    private FCourseHomeworkRecordService courseHomeworkRecordService;

    /**
     * 根据pid和status查询课程列表
     * @param pid
     * @param status
     * @return
     */
    @GetMapping("/pid/{pid}/status/{status}")
    public R getCourseByPidAndStatus(@PathVariable Integer pid, @PathVariable Integer status){
        return R.ok(courseService.selectByPidAndStatus(pid,status));
    }

    /**
     * 根据课程id查询课后作业
     * @param id
     * @return
     */
    @GetMapping("/id/{id}")
    public R getCourseByPidAndStatus(@PathVariable Integer id){
        return R.ok(courseService.selectByPidAndStatus(id));
    }

    /**
     * 查询课后作业和学生回答（根据课程id和学生id）
     * @param courseId  课程id
     * @param studentId 学生id
     * @return
     */
    @GetMapping("/homework")
    public R getHomeworkAnswers(@RequestParam Long courseId, @RequestParam Long studentId) {
        return R.ok(courseHomeworkRecordService.getHomeworkAndAnswers(courseId, studentId));
    }

    /**
     * 批量更新学生作业记录（根据课程id和学生id）
     * @param records
     */
    @PutMapping("/updateBatch")
    public void updateHomeworkBatch(@RequestBody List<FCourseHomeworkRecord> records) {
        courseHomeworkRecordService.updateBatchByHomeworkIdAndSId(records);
    }

    /**
     * 批量添加学生课程作业记录（根据学生id和课程id）
     * @param records
     */
    @PostMapping("/insertRecords")
    public R saveHomeworkRecords(@RequestBody List<FCourseHomeworkRecord> records) {
        return R.ok("提交作业成功",courseHomeworkRecordService.insertHomeworkBatch(records));
    }

    /**
     * 查询学生作业记录（根据学生id和课程id）
     *
     * @param studentId
     * @param homeworkId
     * @return
     */
    @GetMapping("/record")
    public R getRecord(@RequestParam Long studentId, @RequestParam Long homeworkId) {
        return R.ok(courseHomeworkRecordService.getRecordBySIdAndHomeworkId(studentId, homeworkId));
    }

    /**
     * 根据课程id查询课程反馈记录
     * @param id
     * @return
     */
    @GetMapping("/feedback/id/{id}/{studentId}")
    public List<CourseFeedback> getFeedbackByCourseId(@PathVariable Integer id,@PathVariable String studentId){
        return courseFeedbackService.selectFeedbackByCourseId(id,studentId);
    }

    /**
     * 根据学生id和课程id添加课程反馈
     * @param courseFeedback
     * @return
     */
    @PostMapping
    public R insertFeedback(CourseFeedback courseFeedback){
        return R.ok("提交成功",courseFeedbackService.insertFeedbackByStudentIdAndCourseId(courseFeedback));
    }
}
